package com.rssfeed.parser;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;

public class ParseHtmlAsyncTask extends AsyncTask<String, Void, Elements> {

	private ProgressDialog Dialog;
	private String text="";
	private String urltask;
	private Elements link;
	private String tag;
	private Activity _context;
	private GetLinkMp3Callback mLinkMp3Callback; 
	
	public ParseHtmlAsyncTask(Activity _context, GetLinkMp3Callback callback) {
		this._context = _context;
		mLinkMp3Callback = callback;
	}
	
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		Dialog = new ProgressDialog(_context);
		Dialog.setMessage("Loading...");
		Dialog.show();
	}
	
	@Override
	protected Elements doInBackground(String... params) {
		try {
			// Connect to the web site
			urltask = params[0];
			tag = params[1];
			Document document = Jsoup
					.connect(
							urltask)
					.userAgent("Mozilla").get();
			link = document
					.select(tag);

		} catch (IOException e) {
			e.printStackTrace();
		}
		return link;
	}
	
	@Override
	protected void onPostExecute(Elements result) {
		super.onPostExecute(result);
		if (result != null) {
			mLinkMp3Callback.onSuccess();
		} else {
			mLinkMp3Callback.onFailture();
		}
		Dialog.dismiss();
	}
	
//	public interface GetLinkMp3Callback {
//		public void onSuccess();
//		public void onFailture();
//	}
}
